//
//  TMConversationListController.m
//  国学商家管理
//
//  Created by Tao Li on 2018/7/30.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMConversationListController.h"
#import "TMHomeViewModel.h"
@interface TMConversationListController ()<RCIMUserInfoDataSource>
@property (nonatomic, strong) TMHomeViewModel *viewModel;
@end

@implementation TMConversationListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewModel = [[TMHomeViewModel alloc]init];
    //设置需要显示哪些类型的会话,由于楼主只需要单聊功能,所以只设置ConversationType_PRIVATE
    self.title = @"客服消息";
    [self setDisplayConversationTypes:@[@(ConversationType_PRIVATE),
                                        ]];
    
    //设置需要将哪些类型的会话在会话列表中聚合显示
//[self setCollectionConversationType:@[@(ConversationType_PRIVATE),
//                                          ]];
    [self setConversationAvatarStyle:RC_USER_AVATAR_CYCLE];
    self.conversationListTableView.tableFooterView = [UIView new];
    //RCIMUserInfoDataSource
    [[RCIM sharedRCIM] setUserInfoDataSource:self];
    RCUserInfo *info = [[RCUserInfo alloc]initWithUserId:[NSString stringWithFormat:@"%ld",[TMUserManager deaufatManager].userModel.uid] name:[TMUserManager deaufatManager].userModel.userName portrait:[NSString stringWithBaseUrl:[TMUserManager deaufatManager].userModel.userimage]];
    [RCIM sharedRCIM].currentUserInfo = info;
}
- (void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion{

    NSLog(@"%@", userId);
    if ([userId integerValue] == 0) {
        return;
    }
    [self.viewModel getChatInformationWith:[userId integerValue] Success:^(id responseObject) {
        RCUserInfo *infor = [[RCUserInfo alloc]initWithUserId:userId name:responseObject[@"list"][@"username"] portrait:[NSString stringWithBaseUrl:responseObject[@"list"][@"userimage"]]];
        completion(infor);
    } failed:^(NSString *error) {
        
    }];
}

//- (void)refreshUserInfoCache:(RCUserInfo *)userInfo withUserId:(NSString *)userId{//通过单例调用
//      //设置当前的用户信息
//    [RCIM sharedRCIM].currentUserInfo = userInfo;
//    [RCIM sharedRCIM].globalConversationAvatarStyle =RC_USER_AVATAR_CYCLE;//设置头像是圆形还是方形，默认是方形，聊天列表
//    [RCIM sharedRCIM].globalConversationPortraitSize =CGSizeMake(50,50);//设置头像大小，默认是46*46，设置时必须大于36*36，否则无效
//    [RCIM sharedRCIM].globalMessageAvatarStyle =RC_USER_AVATAR_CYCLE;//聊天对话窗口
//    [RCIM sharedRCIM].globalMessagePortraitSize = CGSizeMake(50,50);
//}

//- (void)didReceiveMessageNotification:(NSNotification *)notification{
//
//}

/*!
 即将更新未读消息数的回调，该方法在非主线程回调，如果想在本方法中操作 UI，请手动切换到主线程。
 
 @discussion 当收到消息或删除会话时，会调用此回调，您可以在此回调中执行未读消息数相关的操作。
 */
- (void)notifyUpdateUnreadMessageCount{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self refreshConversationTableViewIfNeeded];
    });
}
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType
         conversationModel:(RCConversationModel *)model
               atIndexPath:(NSIndexPath *)indexPath {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HINDENREDINFORMATIONS" object:nil];
    RCConversationViewController *conversationVC = [[RCConversationViewController alloc]init];
    
    //聊天界面的聊天类型
    conversationVC.conversationType = model.conversationType;
    //需要打开和谁聊天的会话界面,和谁聊天其实是通过TargetId来联系的。
    conversationVC.targetId = model.targetId;
    conversationVC.title = model.conversationTitle;
    [self.navigationController pushViewController:conversationVC animated:YES];
}
#warning 聊天 获取不到头像。
@end
